using UnityEngine;
using System.Collections;

public class CanonballBehavior : MonoBehaviour 
{
	public Vector3 Speed;
	
	//public MeshFilter Cube;
	private float m_ZRotation;
	
	// Use this for initialization
	void Start () 
	{
		gameObject.collider.isTrigger = true;	
	}
	
	// Update is called once per frame
	void Update () 
	{
		//calculate the orientation
		Vector3 Direction = Speed.normalized;
		Vector3 Origin = new Vector3(1, 0, 0);
		
		float cos = Vector3.Dot(Direction, Origin);
		float sin = Vector3.Cross(Origin, Direction).z;
		
		float ArcCos = Mathf.Acos(cos);
		if(sin < 0)
			ArcCos = Mathf.PI * 2 - ArcCos;
		m_ZRotation = ArcCos * 180 / Mathf.PI;
		
		//update the position
		transform.position += Time.deltaTime * Speed;
		transform.rotation = Quaternion.Euler(new Vector3(0, 0, m_ZRotation));
	}
	
	void OnTriggerEnter(Collider other)
	{
		//Debug.Log("trigger collision " + other.gameObject.name);
		if(other.gameObject.layer == LayerMask.NameToLayer("Blocker"))
			this.gameObject.SetActive(false);
	}
}
